﻿using Kiaser.Core.Types;
using Kiaser.DataLibrary;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kiaser.Core
{
   public abstract class BaseManager<T> where T:class
    {
       protected Repository<T> Repository;
       /// <summary>
       /// 默认构造函数
       /// </summary>
       public BaseManager()
           : this(ContextFactory.CurrentContext())
       { 
       
       }
       /// <summary>
       /// 构造函数
       /// </summary>
       /// <param name="dbContext"></param>
       public BaseManager(DbContext dbContext)
       {
           Repository = new Repository<T>(dbContext);
       }

       /// <summary>
       /// 添加
       /// </summary>
       /// <param name="entity">实体数据</param>
       /// <returns>成功时【Data】属性为添加的实体类</returns>
       public virtual Response Add(T entity)
       {
           Response _response = new Response();
           if (Repository.Add(entity)>0)
           {
               _response.Code = 1;
               _response.Message = "添加数据成功";
               _response.Data = entity;
           }
           else
           {
               _response.Code = 0;
               _response.Message = "添加数据失败";
           }
           return _response;
       }

       /// <summary>
       /// 更新
       /// </summary>
       /// <param name="entity">实体类</param>
       /// <returns>成功时【Data】属性为修改的实体类</returns>
       public virtual Response Update(T entity)
       {
           Response _response = new Response();

           if (Repository.Update(entity) > 0)
           {
               _response.Code = 1;
               _response.Message = "修改数据成功";
               _response.Data = entity;
           }
           else
           {
               _response.Code = 0;
               _response.Message = "修改数据失败";
           }

           return _response;
       }

       public virtual Response Remove(int ID)
       {
           Response _reponse = new Response();
           var _entity = Find(ID);

           if (_entity==null)
           {
               _reponse.Code = 10;
               _reponse.Message = "记录不存在";
           }
           else
           {
               if (Repository.Delete(_entity) > 0)
               {
                   _reponse.Code = 1;
                   _reponse.Message = "删除数据成功";
                   _reponse.Data = _entity;
               }
               else
               {
                   _reponse.Code = 0;
                   _reponse.Message = "删除数据失败";
               }
           }
           return _reponse;
       }

       /// <summary>
       /// 查找实体类
       /// </summary>
       /// <param name="ID">主键</param>
       /// <returns>实体</returns>
       public virtual T Find(int ID)
       {
           return Repository.Find(ID);
       }

       /// <summary>
       /// 查找数据列表
       /// </summary>
       /// <returns>所有数据</returns>
       public IQueryable<T> FindList()
       {
           return Repository.FindList();
       }
       /// <summary>
       /// 查找分页数据
       /// </summary>
       /// <param name="paging">分页数据</param>
       /// <returns>分页数据</returns>
       //public Paging<T> FindPageList(Paging<T> paging)
       //{
       //    paging.Items = Repository.FindPageList(paging.PageSize, paging.PageIndex, out paging.TotalNumber).ToList();
       //    return paging;

       //}

       /// <summary>
       /// 总记录数
       /// </summary>
       /// <returns>总记录数</returns>
       public virtual int Count()
       {
           return Repository.Count();
       }
    }
}
